package com.carmaintain.servlets.merchant;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.db.UserLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class UpdateUserPassword
 */
public class UpdateUserPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserPassword() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		String userid = null;
		String oldPassword = "";
		String newPassword = "";
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_WRONG_PARAMATER);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			if(Utils.empty(request.getParameter("merchant_id")) || Utils.empty(request.getParameter("usercode"))){
				result.setCode(Constants.CODE_WRONG_PARAMATER);
				result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else{
				userid = UserLib.checkUserLogin(request.getParameter("merchant_id"), request.getParameter("usercode"), Constants.USER_TYPE_MERCHANT);
				if(request.getParameter("old_password").trim()==null || "".equals(request.getParameter("old_password").trim())){
					result.setCode(Constants.CODE_WRONG_PARAMATER);
					result.setMessage("原始密码不能为空");
					response.setCharacterEncoding("UTF-8");
					response.getWriter().print(Utils.genJsonString(result));
					return;
				}else{
					oldPassword=Utils.md5(request.getParameter("old_password").trim());
					if(!(oldPassword.equals(UserLib.getOldUserPassword(userid)))){
						result.setCode(Constants.CODE_WRONG_PARAMATER);
						result.setMessage("原始密码输入不正确");
						response.setCharacterEncoding("UTF-8");
						response.getWriter().print(Utils.genJsonString(result));
						return;
					}else{
						if(request.getParameter("new_password").trim()==null || "".equals(request.getParameter("new_password").trim())){
							result.setCode(Constants.CODE_WRONG_PARAMATER);
							result.setMessage("新密码不能为空");
							response.setCharacterEncoding("UTF-8");
							response.getWriter().print(Utils.genJsonString(result));
							return;
						}else{
							newPassword=Utils.md5(request.getParameter("new_password").trim());
						}
					}
				}
				
			}
			
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		try {
			if(userid != null){
				UserLib.updateUserPassword(userid, newPassword);
				result.setCode(Constants.CODE_SUCCESS);
				result.setMessage(Constants.MESSAGE_SUCCESS);
			}else{
				result.setCode(Constants.CODE_WRONG_PARAMATER);
				result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
